import { routerRedux } from 'dva/router'
import { parse } from 'qs'
import config from 'config'
import { EnumRoleType } from 'enums'
import { query, logout } from 'services/app'
import * as menuService from 'services/menu'
import { infoDetais } from 'services/search'
import { type, group } from 'services/newfile'
import { userLis, searchs } from 'services/search'
import {
	applicationSave1, applicationUpdate1, changeGzcFile1, changeGzcFileSave1, getGzcFileDetails1, getGzcFileList1, updateFileSave1,
	documentCoverInfo1, documentCoverNew1, documentCoverUpdate1, getCodeNumber1
} from 'services/fileComponent'

const { prefix } = config
const permissions = JSON.parse(sessionStorage.getItem("permissions"))

export default {
	namespace: 'fileComponent',
	state: {
		list: '',
		params: '',
		showDetailsList: false,
		showDetails: false,
		showDashborad: false,
		details: '',
		menu: permissions.indexOf("file:fileComponent:apply") != -1 ? 0 : 1,  //判断权限如果是有申请权限则key为0，无则key为1
		isComponent: "",
		userList: '',
		depTypes: '',
		printDetails: "",
		printInfo: "",
		codeNumber: "",
		coverDetails: "",
		pagination: {
			showSizeChanger: true,
			showQuickJumper: true,
			showTotal: total => `共 ${total} 条`,
			current: 1,
			total: null
		},
	},
	subscriptions: {
		setup({ dispatch, history }) {
			const { query, patch } = location
			history.listen((location) => {
				if (location.pathname === '/file/fileComponent') {
					dispatch({
						type: 'useLists',
						payload: location.query,
					})
					dispatch({
						type: 'depType',
						payload: location.query,
					})
					dispatch({
						type: "fistCom",
					});
				}
			})
		}
	},
	effects: {
		//申请归档
		*applicationSave({ payload }, { call, put }) {
			const data = yield call(applicationSave1, payload)
			yield put({ type: 'showLoading' })
			if (data.success) {
				yield put({ type: 'reload' });
			} else {
				yield put({ type: 'hideLoading' })
			}
		},
		//归档审核
		*applicationUpdate({ payload }, { call, put }) {
			const data = yield call(applicationUpdate1, payload)
			yield put({ type: 'showLoading' })
			if (data.success) {
				yield put({ type: 'reload' });
				yield put({
					type: 'showDetailSuccess',
					payload: {
						showDetails: false
					}
				})
			} else {
				yield put({ type: 'hideLoading' })
			}
		},
		*reload(action, { put, select }) {
			const page = yield select(({ fileComponent }) => fileComponent.pagination)
			const menu = yield select(({ fileComponent }) => fileComponent.menu)
			yield put({
				type: 'searchList', payload: {
					page: page.current,
					operatingType: 0,
					operatingState: menu,
				}
			});
		},
		*searchList({ payload }, { call, put }) {
			const data = yield call(getGzcFileList1, payload)
			yield put({
				type: 'querySuccess',
				payload: {
					params: payload,
					list: data.page.list,
					pagination: {
						total: data.page.totalCount,
						current: data.page.currPage
					}
				}
			})
		},

		*showDet({ payload }, { call, put }) {
			const data = yield call(getGzcFileDetails1, payload)
			yield put({
				type: 'showDetailSuccess',
				payload: {
					showDetails: true,
					details: data
				}
			})
		},
		*showDet1({ payload }, { call, put }) {
			const data = yield call(getGzcFileDetails1, payload)
			yield put({
				type: 'showDetailSuccess1',
				payload: {
					showDetailsList: true,
					details: data
				}
			})
		},

		*showList({ payload }, { call, put }) {
			yield put({
				type: 'showDetailSuccess',
				payload: {
					showDetails: false,
					showDetailsList: false,
					details: "",
				}
			})
		},
		*showMenu({ payload }, { call, put }) {
			yield put({
				type: 'showMenuSuccess',
				payload: {
					menu: payload
				}
			})
		},
		*isFileComponent({ payload }, { call, put }) {
			yield put({
				type: 'showChangeSuccess',
				payload: {
					isComponent: payload
				}
			})
		},
		*useLists({ payload }, { call, put }) {
			const data = yield call(userLis, payload)
			if (data.success) {
				yield put({
					type: 'querySuccess2',
					payload: {
						userList: data.userList
					}
				})
			}
		},
		*depType({ payload }, { call, put }) {
			const data = yield call(type, payload)
			if (data.success) {
				yield put({
					type: "querySuccess7",
					payload: {
						depTypes: data.list
					}
				})
			}
		},
		*update({ payload }, { call, put }) {
			const data = yield call(updateFileSave1, payload)
			if (data) {
				yield put({ type: "showDashboard" })
				yield put({ type: "coverInfo", payload: { documentId: payload.documentId } })
				yield put({ type: "goPrint", payload })
				yield put({ type: "coverInfo", payload })
			}
		},
		*update1({ payload }, { call, put }) {
			const data = yield call(updateFileSave1, payload)
			if (data) {
				yield put({
					type: 'showDetailSuccess',
					payload: {
						showDetails: false,
						showDetailsList: false
					}
				})
				yield put({ type: 'reload' });
			}
		},
		*coverInfo({ payload }, { call, put }) {
			const data = yield call(documentCoverInfo1, { documentId: payload.documentId })
			if (data) {
				yield put({ type: "getCodeNumber" })
			}
			if (data.documentCover) {
				yield put({
					type: "infoSuccess",
					payload: {
						printInfo: data
					}
				})
			}
		},
		*getCoverInfo({ payload }, { call, put }) {
			const data = yield call(documentCoverInfo1, payload)
			if (data) {
				yield put({
					type: "coverSuccess",
					payload: {
						coverDetails: data.documentCover
					}
				})
			}

		},
		*newCover({ payload }, { call, put }) {
			const data = yield call(documentCoverNew1, payload)
			if (data) {
				yield put({ type: 'showLists1' })
				yield put({ type: 'reload' });
			}
		},
		*updateCover({ payload }, { call, put }) {
			const data = yield call(documentCoverUpdate1, payload)
			if (data) {
				yield put({ type: 'showLists1' })
				yield put({ type: 'reload' });
			}
		},
		*getCodeNumber({ }, { call, put }) {
			const data = yield call(getCodeNumber1)
			if (data) {
				yield put({
					type: "getCodeSuccess",
					payload: {
						codeNumber: data.map
					}
				})
			}
		},
		*showLists({ }, { call, put }) {
			yield put({ type: "showLists1" })
			yield put({ type: "reload" })
		}
	},
	reducers: {
		coverSuccess(state, action) {
			const { coverDetails } = action.payload
			return {
				...state,
				coverDetails
			}
		},
		showDashboard(state, action) {
			return {
				...state,
				showDashborad: true,
			}
		},
		querySuccess(state, action) {
			const { params, list, pagination } = action.payload
			return {
				...state,
				params,
				list,
				pagination: {
					...state.pagination,
					...pagination
				}
			}
		},
		showDetailSuccess(state, action) {
			const { showDetailsList, showDetails, details } = action.payload
			return {
				...state,
				showDetails,
				details,
				showDetailsList
			}
		},
		showDetailSuccess1(state, action) {
			const { showDetailsList, details } = action.payload
			return {
				...state,
				showDetailsList,
				details
			}
		},
		showMenuSuccess(state, action) {
			const { menu } = action.payload
			return {
				...state,
				menu
			}
		},
		showChangeSuccess(state, action) {
			const { isComponent } = action.payload
			return {
				...state,
				isComponent
			}
		},
		querySuccess2(state, action) {
			const { userList } = action.payload
			return {
				...state,
				userList
			}
		},
		querySuccess7(state, action) {
			const { depTypes } = action.payload
			return {
				...state,
				depTypes
			}
		},
		hideLoading(state) {
			return { ...state, loading: false }
		},
		showLoading(state) {
			return { ...state, loading: true }
		},
		goPrint(state, { payload }) {
			return {
				...state,
				printDetails: payload,
			}
		},
		infoSuccess(state, action) {
			const { printInfo } = action.payload
			return {
				...state,
				printInfo
			}
		},
		showLists1(state, action) {
			return {
				...state,
				showDashborad: false,
				showDetails: false,
				showDetailsList: false,
				printDetails:'',
			}
		},
		getCodeSuccess(state, action) {
			const { codeNumber } = action.payload
			return {
				...state,
				codeNumber
			}
		},
		fistCom(state, { payload }) {
			return {
				...state,
				showDetailsList: false,
				showDetails: false,
				showDashborad: false,
			}
		}
	},
}
